﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Gp.Scripts.Data;
using Gp.Scripts.Data.Equip;
using Gp.Scripts.Core;
using UnityEngine;
using Random = UnityEngine.Random;


namespace Gp.AI {
    [Serializable]
    [CreateNodeMenu("一般行为/攻击")]
    public class AttackAINode : NormalAINode {
        public override AINodeEntry NodeEntry() {
            return new AttackAINodeEntry(this);
        }
    }


    public class AttackAINodeEntry : NormalAINodeEntry<AttackAINode> {
        private const int OrgWeight = 10;

        public override IEnumerator OnExecute() {
            // var placeHolder = CellGridSystem.Instance.PlaceHolder;
            // var atkSkill = Unit.ComSkill.WeaponAttack;
            // var units = Global.Get<BattleSystem>().BattleSettle.GetUnitsByPlayer(PlayerConst.PlayerUnitNumber);
            //
            // List<PairItem> map = new();
            // string debugInfo = "";
            //
            // int total = 0;
            //
            // var targets = new List<AttachCell>(atkSkill.AvailableTargets());
            //
            // foreach (var u in units) {
            //     var ls = GetWeightDic(u);
            //     var sum = ls.Sum(i => i.num);
            //
            //     // // 筛除无法攻击的目标
            //     // if (!targets.Contains(u.CurrentCell)) continue;
            //
            //
            //     debugInfo += $"\nAI: 可攻击目标 [{u.UnitName}] 权重 ---- {sum}\n";
            //     total += sum * sum;
            //     ls.ForEach(i => debugInfo += $"\t[{i.weight.Label()}]: {i.num}\n");
            //
            //
            //     map.Add(new PairItem(u, ls, sum));
            // }
            //
            // var range = Random.Range(0, total);
            // BaseUnit selected = null;
            // foreach (var item in map) {
            //     range -= item.Sum * item.Sum;
            //     if (range >= 0) continue;
            //
            //
            //     selected = item.Unit;
            //     break;
            // }
            //
            // if (selected == null) {
            //     yield break;
            // }
            //
            // debugInfo += $"\n 选择结果： {selected.UnitName}";
            // Debug.Log(debugInfo);
            // yield return atkSkill.AIExecute(new SkillContext(){TargetUnit = selected});
            yield break;
        }


        private List<WeightInfo> GetWeightDic(BaseUnit target) {
            var res = new List<WeightInfo>();

            // ================================== 位置权重 =================================================
            res.Add(GetWeight(AtkWeight.BackPos));


            // ================================== 生命值权重 =================================================
            var units = Global.Get<BattleSystem>().BattleSettle.GetUnitsByPlayer(PlayerConst.PlayerUnitNumber).ToList();
            units.Sort((a1, a2) => Cal(a1) - Cal(a2));
            // 目标为生命值 + 护甲值最低
            bool flag = units.Count > 0 && units[0] == target;
            if (flag) {
                res.Add(GetWeight(AtkWeight.Vulnerable));
            }


            // ================================== 护盾权重 ==================================================
            if (target.ComEquip.HasShield()) {
                res.Add(GetWeight(AtkWeight.Shield));
            }


            // ================================== 负面状态 ==================================================
            if (target.ComBuff.Buffs.Any(x => x.BuffTags().Contains(BuffTags.Negative))) {
                res.Add(GetWeight(AtkWeight.BeNegative));
            }

            // ================================== 缺少防御点 ==================================================
            int delta = 2 - target.ComCost.GetResource(CostField.Reaction);
            if (delta > 0) {
                var temp = GetWeight(AtkWeight.LackDefence);
                temp.num *= delta;
                res.Add(temp);
            }

            // ================================== 没有格挡层数 ==================================================
            // var buff = target.ComBuff.GetBuff<BlockAttackBuff>();
            // if (buff is {Level: > 0}) {
            //     res.Add(GetWeight(AtkWeight.LackBlock));
            // }

            // ================================== 持有重型武器 ==================================================
            // if (target.ComEquip.Weapons.Any(x => x.WeaponTags().Contains(WeaponTag.Heavy))) {
                // res.Add(GetWeight(AtkWeight.HeavyWeapon));
            // }


            return res;

            int Cal(BaseUnit u) {
                return u.CurHealth.Value + u.ComEquip.GetArmorPoint();
            }
        }


        public override bool ShouldPerform() {
            return Unit.ComSkill.WeaponAttack.CanSelect().Result();
        }


        protected override AIActionArgs GetActionInfo() {
            return AIActionArgs.Build(Unit.ComSkill.WeaponAttack);
        }




        private WeightInfo GetWeight(AtkWeight weight) {
            return StartNode.attackWeight.GetWeight(weight);
        }
        
        
        

        public AttackAINodeEntry(AttackAINode data) : base(data) { }


        private struct PairItem {
            public BaseUnit Unit;
            public List<WeightInfo> Info;

            public int Sum;

            // public int WeaponIndex;
            public PairItem(BaseUnit unit, List<WeightInfo> info, int sum) {
                Unit = unit;
                Info = info;
                Sum = sum;
                // WeaponIndex = weaponIndex;
            }
        }
    }
}